package com.example.onlinemusic.controller;

import com.example.onlinemusic.mapper.LoveMusicMapper;
import com.example.onlinemusic.model.LoveMusic;
import com.example.onlinemusic.model.Music;
import com.example.onlinemusic.model.User;
import com.example.onlinemusic.tools.Constant;
import com.example.onlinemusic.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 74646
 * Date: 2022-08-24
 * Time: 12:13
 */
@RestController
@RequestMapping("/lovemusic")
public class LoveMusicController {

    @Autowired
    private LoveMusicMapper loveMusicMapper;

    /**
     * 收藏音乐
     * 请求路径：/lovemusic/likeMusic?music_id=x
     * @param music_id 传入的音乐 id
     * @param request
     * @return
     */
    @RequestMapping("/likeMusic")
    public ResponseBodyMessage<Boolean> likeMusic(@RequestParam String music_id, HttpServletRequest request){

        // 1. 检查是否登录
        HttpSession session = request.getSession(false);
        if(session == null || session.getAttribute(Constant.USER_SESSION_KEY)==null){
            System.out.println("登录失败");
            return  new ResponseBodyMessage<>(-200,"请登录后再进行收藏",false);
        }

        // 2. 获取 usrId
        User user = (User)session.getAttribute(Constant.USER_SESSION_KEY);
        int userId = user.getUserId();
        System.out.println("在控制台打印日志：user_id："+userId);

        // 3. 获取 musicId
        int musicId = Integer.parseInt(music_id);
        System.out.println("在控制台打印日志：music_id："+musicId);

        // 4. 判断当前歌曲是否被收藏过
        LoveMusic loveMusic = loveMusicMapper.findLoveMusic(userId,musicId);
        if(loveMusic != null){
            // 之前收藏过这首歌曲
            return new ResponseBodyMessage<>(-200,"该歌曲已被收藏，请勿重复收藏",false);
        }else{
            boolean ret =  loveMusicMapper.insertLoveMusic(userId,musicId);
            if(ret){
                return new ResponseBodyMessage<>(200,"收藏成功",true);
            }else{
                return new ResponseBodyMessage<>(-200,"收藏失败",false);
            }
        }
    }

    @RequestMapping("findlovemusic")
    public ResponseBodyMessage<List<Music>> findLoveMusic(@RequestParam(required = false) String musicName,HttpServletRequest request){
        // 1. 检查是否登录
        HttpSession session = request.getSession(false);
        if(session == null || session.getAttribute(Constant.USER_SESSION_KEY)==null){
            System.out.println("登录失败");
            return  new ResponseBodyMessage<>(-200,"请登录后再查找",null);
        }

        // 2. 获取 usrId
        User user = (User)session.getAttribute(Constant.USER_SESSION_KEY);
        int user_id = user.getUserId();
        System.out.println("在控制台打印日志：user_id："+user_id);

        List<Music> musicList = null;
        if(musicName == null){
            //查询当前用户收藏的所有音乐
            musicList = loveMusicMapper.findLoveMusicByUserId(user_id);
        }else{
            //模糊查询，根据歌曲名查询当前用户指定的收藏歌曲
            musicList = loveMusicMapper.findLoveMusicByMusicNameAndUserId(musicName,user_id);
        }
        return new ResponseBodyMessage<>(200,"查询成功",musicList);
    }

    /**
     * 移除收藏的音乐
     * @param music_id
     * @param request
     * @return
     */
    @RequestMapping("/removelovemusic")
    public ResponseBodyMessage<Boolean> removeLoveMusic(@RequestParam String music_id,HttpServletRequest request){

        // 1. 检查是否登录
        HttpSession session = request.getSession(false);
        if(session == null || session.getAttribute(Constant.USER_SESSION_KEY)==null){
            System.out.println("登录失败");
            return  new ResponseBodyMessage<>(-200,"请登录后再移除",false);
        }

        // 2. 获取 usrId
        User user = (User)session.getAttribute(Constant.USER_SESSION_KEY);
        int userId = user.getUserId();
        System.out.println("在控制台打印日志：user_id："+userId);

        //  3. 将传入的 music_id 转换成整型（获取 musicId）
        int musicId = Integer.parseInt(music_id);
        System.out.println("在控制台打印日志：music_id："+musicId);

        int ret = loveMusicMapper.removeLoveMusic(userId,musicId);
        if(ret == 1){
            return new ResponseBodyMessage<>(200,"取消收藏成功",true);
        }else{
            return new ResponseBodyMessage<>(-200,"取消收藏失败",false);
        }
    }

}


















